package cn.xfdzx.android.apps.shop.fragment.home;

import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.google.android.material.appbar.AppBarLayout;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.loader.ImageLoader;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.FamilyBuyActivity;
import cn.xfdzx.android.apps.shop.activity.HomeSearchActivity;
import cn.xfdzx.android.apps.shop.activity.HomeSearch_NewRetailActivity;
import cn.xfdzx.android.apps.shop.activity.LocationActivity;
import cn.xfdzx.android.apps.shop.activity.MessageActivity;
import cn.xfdzx.android.apps.shop.activity.RetailLevelActivity;
import cn.xfdzx.android.apps.shop.activity.ShopActivity;
import cn.xfdzx.android.apps.shop.activity.login.OneLoginMainActivity;
import cn.xfdzx.android.apps.shop.activity.test.TestActivity;
import cn.xfdzx.android.apps.shop.adapter.FoundGoodTitleAdapter;
import cn.xfdzx.android.apps.shop.adapter.HomeClassGVAdapter;
import cn.xfdzx.android.apps.shop.adapter.HomeVPAdapter;
import cn.xfdzx.android.apps.shop.adapter.RetailFamilyGoodAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpFragment;
import cn.xfdzx.android.apps.shop.bean.ConfirmGoodBeen;
import cn.xfdzx.android.apps.shop.bean.FamilyBuyBeen;
import cn.xfdzx.android.apps.shop.bean.FoundGoodTitleBeen;
import cn.xfdzx.android.apps.shop.bean.HomeRetailBeen;
import cn.xfdzx.android.apps.shop.bean.NearShopBeen;
import cn.xfdzx.android.apps.shop.contract.HomeRetailContract;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.contract.NearClickListener;
import cn.xfdzx.android.apps.shop.contract.ScrollClickListener;
import cn.xfdzx.android.apps.shop.evenbus.CutHomeTypeMessage;
import cn.xfdzx.android.apps.shop.evenbus.FoundGoodSelectIdMessage;
import cn.xfdzx.android.apps.shop.evenbus.HomeAdressMessage;
import cn.xfdzx.android.apps.shop.evenbus.HomeFamilyMessage;
import cn.xfdzx.android.apps.shop.evenbus.HomeInitDataMessage;
import cn.xfdzx.android.apps.shop.evenbus.HomeTypeMessage;
import cn.xfdzx.android.apps.shop.evenbus.NearShopSelectMessage;
import cn.xfdzx.android.apps.shop.evenbus.PostCarNumMessage;
import cn.xfdzx.android.apps.shop.evenbus.UpdateCarNumMessage;
import cn.xfdzx.android.apps.shop.presenter.HomeRetailPresenter;
import cn.xfdzx.android.apps.shop.util.CommonUtil;
import cn.xfdzx.android.apps.shop.util.GlideUtils;
import cn.xfdzx.android.apps.shop.util.ItemOffsetDecoration;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.StatusBarUtil;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.util.UtilsData;
import cn.xfdzx.android.apps.shop.view.CheckableTextView;
import cn.xfdzx.android.apps.shop.view.IArcView;
import cn.xfdzx.android.apps.shop.view.MyGridView;
import cn.xfdzx.android.apps.shop.view.ZhangPhilScrollView;

import static cn.xfdzx.android.apps.shop.activity.MainActivity.TYPE_IS_RETAIL;
import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;
import static cn.xfdzx.android.apps.shop.apps.Constants.SHOPID_69_NO;

/**
 * @author gzk
 * @date 2020/3/26 0026
 * description：社区零售
 */
public class HomeCommunityRetailFragment extends BaseMvpFragment<HomeRetailPresenter> implements HomeRetailContract.View, NearClickListener, View.OnClickListener {


    //find view id
    private ImageView ivLocation;
    private LinearLayout llState;
    private ImageView ivMessage;//消息
    private TextView tvHomeAddress;//定位地址
    private LinearLayout llHomeSearch;//搜索响应
    private Banner banner;//轮播图
    private MyGridView gvHomeClass;//五个分类
    //    private ImageView ivHomeActivity;//首页图片活动
    private ImageView ivHomeDay_1;//每日特价1
    private ImageView ivHomeDay_2;//每日特价2
    //    private IArcView iavBg;//椭圆背景
    private AppBarLayout appBar;
    private RelativeLayout.LayoutParams item_bar;
    private HomeClassGVAdapter homeClassGVAdapter;
    private Context mContext;

    private float LL_SEARCH_MIN_TOP_MARGIN, LL_SEARCH_MAX_TOP_MARGIN, LL_SEARCH_MAX_WIDTH, LL_SEARCH_MIN_WIDTH, TV_TITLE_MAX_TOP_MARGIN;
    private ViewGroup.MarginLayoutParams searchLayoutParams, titleLayoutParams;


    private String addressMessage;//当前地址
    private List<Fragment> mFragments;
    private String[] title = {"发现好货", "附近商店"};
    private HomeVPAdapter homeVPAdapter;
    private ViewPager vpMainOne;
    private RelativeLayout rlFoundGood, rlNearShop;
    private TextView tvFoundGood, tvNearShop;
    private View vFoundGood, vNearShop;
    //发现好货标题
    private RecyclerView rvFoundGoodTitle;
    private FoundGoodTitleAdapter foundGoodTitleAdapter;
    private List<FoundGoodTitleBeen.DataBean> mList = new ArrayList<>();
    private String select_id = "";
    //附近商店
    private RelativeLayout rlNearTitle;
    private CheckableTextView tvHomeShopComprehensiveSequencing, tvHomeShopSales, tvHomeShopDistance;
    private boolean isComprehensiveSequencing = true;
    private boolean isSales = false;
    private boolean isDistance = false;
    SmartRefreshLayout mRefreshLayout;
    //家庭购
    private List<FamilyBuyBeen.DataBean.FamilyShoppingActivityBean> mFamilyList = new ArrayList<>();
    private RecyclerView rvFamilyBuy;
    private RelativeLayout rlFamilyBuy;
    private TextView tvFamilyMore;
    private RetailFamilyGoodAdapter retailFamilyGoodAdapter;
    private ImageView viewHomeGif;
    boolean flag=true;  //默认进来 请求一下附近商店 、下的综合排序

    @Override
    protected void initView(View view) {
        mPresenter = new HomeRetailPresenter();
        mPresenter.attachView(this);

        mContext = getActivity();
        EventBus.getDefault().register(this);
        initFindViewId(view);
        initHigh();
        initFragment();

        mPresenter.initData();
        mPresenter.initTitle("-1", "4");
        mPresenter.familyBuy("0");
        GlideUtils.loadGifResource(getActivity(), R.drawable.icon_retail_image, viewHomeGif);

    }

    private void initFragment() {
        mFragments = new ArrayList<>();
        mFragments.add(RetailFoundGoodFragment.newInstance(1));
        mFragments.add(RetailNearShopFragment.newInstance(1));

        homeVPAdapter = new HomeVPAdapter(getChildFragmentManager(), title, mFragments);
        vpMainOne.setAdapter(homeVPAdapter);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(getActivity(), R.dimen.item_offset);
        rvFamilyBuy.setLayoutManager(linearLayoutManager);
        rvFamilyBuy.addItemDecoration(itemDecoration);
        retailFamilyGoodAdapter = new RetailFamilyGoodAdapter(getActivity(), mFamilyList, mPresenter);
        retailFamilyGoodAdapter.setOnItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(getActivity(), ShopActivity.class);
                intent.putExtra("good_id", mFamilyList.get(position).getGoods_id() + "");
                intent.putExtra("shop_id", mFamilyList.get(position).getRole_id() + "");

                startActivity(intent);
            }
        });
        rvFamilyBuy.setAdapter(retailFamilyGoodAdapter);


    }

    private void initFindViewId(View view) {
        viewHomeGif = view.findViewById(R.id.view_home_gif);
        mRefreshLayout = view.findViewById(R.id.refreshLayout);
        ivLocation = view.findViewById(R.id.iv_location);
        llState = view.findViewById(R.id.ll_state);
        appBar = view.findViewById(R.id.app_bar);
        ivMessage = view.findViewById(R.id.iv_message);
        tvHomeAddress = view.findViewById(R.id.tv_home_address);
        llHomeSearch = view.findViewById(R.id.ll_home_search);
        banner = view.findViewById(R.id.ll_banner);
        gvHomeClass = view.findViewById(R.id.gv_home_class);
//        ivHomeActivity = view.findViewById(R.id.iv_home_activity);
        ivHomeDay_1 = view.findViewById(R.id.iv_home_day_1);
        ivHomeDay_2 = view.findViewById(R.id.iv_home_day_2);
//        iavBg = view.findViewById(R.id.iav_bg);
        rlFoundGood = view.findViewById(R.id.rl_found_good);
        tvFoundGood = view.findViewById(R.id.tv_found_good);
        vFoundGood = view.findViewById(R.id.view_found_good);
        rlNearShop = view.findViewById(R.id.rl_near_shop);
        tvNearShop = view.findViewById(R.id.tv_near_shop);
        vNearShop = view.findViewById(R.id.view_near_shop);
        vpMainOne = view.findViewById(R.id.vp_main_one);
        rvFoundGoodTitle = view.findViewById(R.id.rv_found_good_title);
        rlNearTitle = view.findViewById(R.id.rl_near_title);
        tvHomeShopComprehensiveSequencing = view.findViewById(R.id.tv_home_shop_comprehensive_sequencing);
        tvHomeShopSales = view.findViewById(R.id.tv_home_shop_sales);
        tvHomeShopDistance = view.findViewById(R.id.tv_home_shop_distance);
        rvFamilyBuy = view.findViewById(R.id.rv_family_buy);
        rlFamilyBuy = view.findViewById(R.id.rl_family_buy);
        tvFamilyMore = view.findViewById(R.id.tv_family_more);
//        iavBg.setStringColor("#FB9129");
        bannerInit();

        ivLocation.setOnClickListener(this);
        tvHomeAddress.setOnClickListener(this);
        llHomeSearch.setOnClickListener(this);
        ivMessage.setOnClickListener(this);
        rlFoundGood.setOnClickListener(this);
        rlNearShop.setOnClickListener(this);
        tvHomeShopComprehensiveSequencing.setOnClickListener(this);
        tvHomeShopSales.setOnClickListener(this);
        tvHomeShopDistance.setOnClickListener(this);
        tvFamilyMore.setOnClickListener(this);
        viewHomeGif.setOnClickListener(this);


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rvFoundGoodTitle.setLayoutManager(linearLayoutManager);
        foundGoodTitleAdapter = new FoundGoodTitleAdapter(getActivity(), mList);
        foundGoodTitleAdapter.setOnItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (select_id.equals(mList.get(position).getId() + "")) {
                    select_id = "";
                } else {
                    select_id = mList.get(position).getId() + "";
                }
                foundGoodTitleAdapter.setSelect_id(select_id);
                EventBus.getDefault().post(new FoundGoodSelectIdMessage(select_id));
            }
        });
        rvFoundGoodTitle.setAdapter(foundGoodTitleAdapter);

        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.initData();
                mPresenter.initTitle("-1", "4");
                //刷新发现好货下的,传空字符就是默认
                foundGoodTitleAdapter.setSelect_id("");
                EventBus.getDefault().post(new FoundGoodSelectIdMessage(""));
                //刷新附近商店下的
                isComprehensiveSequencing = true;
                isSales = false;
                isDistance = false;
                public_set();
                EventBus.getDefault().post(new NearShopSelectMessage(1));
                mRefreshLayout.finishRefresh(300);
            }
        });


    }

    private void initHigh() {
        item_bar = (RelativeLayout.LayoutParams) llState.getLayoutParams();
        item_bar.height = StatusBarUtil.getStatusBarHeight(mContext);
        llState.setLayoutParams(item_bar);

        searchLayoutParams = (ViewGroup.MarginLayoutParams) llHomeSearch.getLayoutParams();
        titleLayoutParams = (ViewGroup.MarginLayoutParams) tvHomeAddress.getLayoutParams();

        LL_SEARCH_MIN_TOP_MARGIN = CommonUtil.dp2px(mContext, 8f);//布局关闭时顶部距离
        LL_SEARCH_MAX_TOP_MARGIN = CommonUtil.dp2px(mContext, 48f);//布局默认展开时顶部距离
        LL_SEARCH_MAX_WIDTH = CommonUtil.getScreenWidth(mContext) - CommonUtil.dp2px(mContext, 30f);//布局默认展开时的宽度
        LL_SEARCH_MIN_WIDTH = CommonUtil.getScreenWidth(mContext) - CommonUtil.dp2px(mContext, 82f);//布局关闭时的宽度
        TV_TITLE_MAX_TOP_MARGIN = CommonUtil.dp2px(mContext, 11f);
        //Todo 把这个监听注释
        appBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
                float dy = -i * 0.65f;
                float searchLayoutNewTopMargin = LL_SEARCH_MAX_TOP_MARGIN - dy;
                float searchLayoutNewWidth = LL_SEARCH_MAX_WIDTH - dy * 1.3f;//此处 * 1.3f 可以设置搜索框宽度缩放的速率

                float titleNewTopMargin = (float) (TV_TITLE_MAX_TOP_MARGIN - dy * 0.5);

                //处理布局的边界问题
                searchLayoutNewWidth = searchLayoutNewWidth < LL_SEARCH_MIN_WIDTH ? LL_SEARCH_MIN_WIDTH : searchLayoutNewWidth;

                if (searchLayoutNewTopMargin < LL_SEARCH_MIN_TOP_MARGIN) {
                    searchLayoutNewTopMargin = LL_SEARCH_MIN_TOP_MARGIN;
                }

                if (searchLayoutNewWidth < LL_SEARCH_MIN_WIDTH) {
                    searchLayoutNewWidth = LL_SEARCH_MIN_WIDTH;
                }

                float titleAlpha = 255 * titleNewTopMargin / TV_TITLE_MAX_TOP_MARGIN;
                if (titleAlpha < 0) {
                    titleAlpha = 0;
                }

                //设置相关控件的LayoutParams  此处使用的是MarginLayoutParams，便于设置params的topMargin属性
                tvHomeAddress.setTextColor(tvHomeAddress.getTextColors().withAlpha((int) titleAlpha));
                titleLayoutParams.topMargin = (int) titleNewTopMargin;
                tvHomeAddress.setLayoutParams(titleLayoutParams);

                searchLayoutParams.topMargin = (int) searchLayoutNewTopMargin;
                searchLayoutParams.width = (int) searchLayoutNewWidth;
                llHomeSearch.setLayoutParams(searchLayoutParams);
            }
        });
        vpMainOne.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    tvFoundGood.setTextColor(getResources().getColor(R.color.color_212121));
                    tvNearShop.setTextColor(getResources().getColor(R.color.color_545454));
                    vFoundGood.setVisibility(View.VISIBLE);
                    vNearShop.setVisibility(View.GONE);
                    rvFoundGoodTitle.setVisibility(View.VISIBLE);
                    rlNearTitle.setVisibility(View.GONE);
                } else {
                    tvFoundGood.setTextColor(getResources().getColor(R.color.color_545454));
                    tvNearShop.setTextColor(getResources().getColor(R.color.color_212121));
                    vFoundGood.setVisibility(View.GONE);
                    vNearShop.setVisibility(View.VISIBLE);
                    rvFoundGoodTitle.setVisibility(View.GONE);
                    rlNearTitle.setVisibility(View.VISIBLE);

                    if ( flag) {
                        EventBus.getDefault().post(new NearShopSelectMessage(1));
                        flag=false;
                    }

                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        if (TYPE_IS_RETAIL == 0) {
            tvFoundGood.setTextColor(getResources().getColor(R.color.color_212121));
            tvNearShop.setTextColor(getResources().getColor(R.color.color_545454));
            vFoundGood.setVisibility(View.VISIBLE);
            vNearShop.setVisibility(View.GONE);
            rvFoundGoodTitle.setVisibility(View.VISIBLE);
            rlNearTitle.setVisibility(View.GONE);
        } else {
            tvFoundGood.setTextColor(getResources().getColor(R.color.color_545454));
            tvNearShop.setTextColor(getResources().getColor(R.color.color_212121));
            vFoundGood.setVisibility(View.GONE);
            vNearShop.setVisibility(View.VISIBLE);
            rvFoundGoodTitle.setVisibility(View.GONE);
            rlNearTitle.setVisibility(View.VISIBLE);
        }

        isComprehensiveSequencing = true;
        isSales = false;
        isDistance = false;
        public_set();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home_community_retail_new;
    }

    @Override
    public void showLoading() {
        if (TYPE_IS_RETAIL == 0) {
            if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
                ProgressDialog.getInstance().show(getActivity());
            }
        }
    }

    @Override
    public void hideLoading() {
        if (TYPE_IS_RETAIL == 0) {
            ProgressDialog.getInstance().dismiss();
        }
    }

    @Override
    public void onError(Throwable throwable) {
//        Toast.makeText(getActivity(), "网络请求出错", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStart() {
        super.onStart();
        //开始轮播
        banner.startAutoPlay();
    }

    @Override
    public void onStop() {
        //结束轮播
        banner.stopAutoPlay();
        super.onStop();
    }

    //发现好货下的分类
    @Override
    public void onInitTitleSuccess(FoundGoodTitleBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            mList.clear();
            mList.addAll(bean.getData());
            foundGoodTitleAdapter.notifyDataSetChanged();
        } else {
            Toast.makeText(getActivity(), bean.getMsg(), Toast.LENGTH_SHORT).show();
        }

    }

    //家庭购
    @Override
    public void onFamilyBuySuccess(FamilyBuyBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            mFamilyList.clear();
            mFamilyList.addAll(bean.getData().getFamily_shopping_activity());
            if (mFamilyList.size() == 0) {
                rlFamilyBuy.setVisibility(View.GONE);
            } else {
                rlFamilyBuy.setVisibility(View.VISIBLE);
            }
            retailFamilyGoodAdapter.notifyDataSetChanged();
        } else {
            Toast.makeText(getActivity(), bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onAddCarSuccess(ConfirmGoodBeen bean, int position, int total_num) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            Toast.makeText(getActivity(), "加入购物车成功", Toast.LENGTH_SHORT).show();
            mFamilyList.get(position).setCart_num(total_num);
            retailFamilyGoodAdapter.notifyDataSetChanged();
            EventBus.getDefault().post(new PostCarNumMessage());

        } else {
            if (bean.getStatus() == Constants.LOGIN_CODE) {
                UtilsData.getPreference(getActivity(), "login").remove("head_url");
                UtilsData.getPreference(getActivity(), "login").remove("nickname");
                UtilsData.getPreference(getActivity(), "login").remove("is_store");
                UtilsData.getPreference(getActivity(), "login").remove("token");
                APP_TOKEN = "";
                Intent intent = new Intent(getActivity(), OneLoginMainActivity.class);
                startActivity(intent);
                return;
            }
            Toast.makeText(getActivity(), bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onInitDataSuccess(HomeRetailBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            //69活动的开关
            UtilsData.getPreference(getActivity(), "activity").put("is_69", bean.getData().isActivity_goods_buy_give());
            SHOPID_69_NO.clear();
            SHOPID_69_NO.addAll(bean.getData().getActivity_goods_buy_give_data().getNo_participate_store_id());
            homeBannerData(bean.getData().getBanners(), bean.getData().getIndexWholesaleCategory());
            everyDayData(bean.getData().getIndexWholesaleCategory());
        } else {
            Toast.makeText(getActivity(), bean.getMsg(), Toast.LENGTH_SHORT).show();
        }

    }

    private void everyDayData(List<HomeRetailBeen.DataBean.IndexWholesaleCategoryBean> indexList) {
        ivHomeDay_1.setImageResource(R.drawable.image_home_day_1);
        ivHomeDay_2.setImageResource(R.drawable.image_home_day_2);
        ivHomeDay_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.isFastClick()) {
                    for (HomeRetailBeen.DataBean.IndexWholesaleCategoryBean bean : indexList) {
                        if (bean.getName().equals("新鲜蔬菜")) {
                            Intent intent = new Intent(getActivity(), RetailLevelActivity.class);
                            intent.putExtra("title_name", bean.getName());
                            intent.putExtra("type_id", bean.getId() + "");
                            startActivity(intent);
                        }
                    }

                }
            }
        });
        ivHomeDay_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.isFastClick()) {
                    for (HomeRetailBeen.DataBean.IndexWholesaleCategoryBean bean : indexList) {
                        if (bean.getName().equals("时令水果")) {
                            Intent intent = new Intent(getActivity(), RetailLevelActivity.class);
                            intent.putExtra("title_name", bean.getName());
                            intent.putExtra("type_id", bean.getId() + "");
                            startActivity(intent);
                        }
                    }
                }
            }
        });
    }


    private void homeBannerData(List<HomeRetailBeen.DataBean.BannersBean> beanList, List<HomeRetailBeen.DataBean.IndexWholesaleCategoryBean> indexList) {
        List<String> imageUrls = new ArrayList<>();
        for (int i = 0; i < beanList.size(); i++) {
            imageUrls.add(beanList.get(i).getPic());
        }
        //设置图片网址或地址的集合
        banner.setImages(imageUrls);
        //必须最后调用的方法，启动轮播图。
        banner.start();
        homeClassGVAdapter.setData(indexList);
        gvHomeClass.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getActivity(), RetailLevelActivity.class);
                intent.putExtra("title_name", indexList.get(i).getName());
                intent.putExtra("type_id", indexList.get(i).getId() + "");
                startActivity(intent);
            }
        });
        //广告图
//        ivHomeActivity.setBackgroundResource(R.drawable.image_shichang_center);
    }


    private void bannerInit() {
        //设置内置样式，共有六种可以点入方法内逐一体验使用。
        banner.setBannerStyle(BannerConfig.NOT_INDICATOR);
        //设置图片加载器，图片加载器在下方
        banner.setImageLoader(new MyLoader());
        //设置轮播的动画效果，内含多种特效，可点入方法内查找后内逐一体验
        banner.setBannerAnimation(Transformer.Default);
        //设置轮播间隔时间
        banner.setDelayTime(3000);
        //设置是否为自动轮播，默认是“是”。
        banner.isAutoPlay(true);
        homeClassGVAdapter = new HomeClassGVAdapter(getActivity());
        gvHomeClass.setAdapter(homeClassGVAdapter);
        gvHomeClass.setSelector(new ColorDrawable(Color.TRANSPARENT));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_location:
            case R.id.tv_home_address:
                if (Utils.isFastClick()) {
                    Intent intent = new Intent(getActivity(), LocationActivity.class);
                    intent.putExtra("pageForm", "first");
                    intent.putExtra("send_to_address", addressMessage);
                    startActivity(intent);
                }
                break;
            case R.id.ll_home_search:
                if (Utils.isFastClick()) {

                    //Intent home_search = new Intent(getActivity(), HomeSearchActivity.class);
                    Intent home_search = new Intent(getActivity(), HomeSearch_NewRetailActivity.class);
                    startActivity(home_search);
                }
                break;
            case R.id.iv_message:
                //跳转消息

                Intent intent = new Intent(getActivity(), TestActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_found_good:
                vpMainOne.setCurrentItem(0);
                break;
            case R.id.rl_near_shop:
                vpMainOne.setCurrentItem(1);


                break;
            case R.id.tv_home_shop_comprehensive_sequencing://综合排序
                if (!isComprehensiveSequencing) {
                    isComprehensiveSequencing = true;
                    isSales = false;
                    isDistance = false;
                    public_set();
                    EventBus.getDefault().post(new NearShopSelectMessage(1));
                }
                break;
            case R.id.tv_home_shop_sales://销量
                if (!isSales) {
                    isComprehensiveSequencing = false;
                    isSales = true;
                    isDistance = false;
                    public_set();
                    EventBus.getDefault().post(new NearShopSelectMessage(2));
                }
                break;
            case R.id.tv_home_shop_distance://距离
                if (!isDistance) {
                    isComprehensiveSequencing = false;
                    isSales = false;
                    isDistance = true;
                    public_set();
                    EventBus.getDefault().post(new NearShopSelectMessage(3));
                }
                break;
            case R.id.view_home_gif://
            case R.id.tv_family_more://
                intent = new Intent(getActivity(), FamilyBuyActivity.class);
                startActivity(intent);
                break;


        }
    }

    private void public_set() {
        tvHomeShopComprehensiveSequencing.setChecked(isComprehensiveSequencing);
        tvHomeShopSales.setChecked(isSales);
        tvHomeShopDistance.setChecked(isDistance);
    }

    //更新地址
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void XXX(HomeAdressMessage messageEvent) {
        Log.e("messageEvent", messageEvent.getLng() + "--经纬onefragment度--" + messageEvent.getLat());
        addressMessage = messageEvent.getMessage();
        tvHomeAddress.setText(messageEvent.getMessage());
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void XXXXXX(HomeFamilyMessage messageEvent) {
        mPresenter.familyBuy("0");
    }

    @Override
    public void onDestroyView() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroyView();
    }

    //自定义的图片加载器
    private class MyLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            RoundedCorners roundedCorners = new RoundedCorners(40);
            //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
            RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
            options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                    .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                    .error(R.drawable.icon_defult);
            imageView.setPadding(50, 0, 50, 0);
            Glide.with(context).load(path).apply(options.transform(new CenterCrop(), roundedCorners)).into(imageView);
        }

    }


    @Override
    public void finishRefresh() {
        mRefreshLayout.finishRefresh(true);//刷新完成
    }

    @Override
    public void finishLoadMore() {
        mRefreshLayout.finishLoadMore(true);//加载完成
    }

    public static HomeCommunityRetailFragment newInstance() {
        Bundle bundle = new Bundle();
//        bundle.putString("item", "UserPostFragment");
//        bundle.putLong(Constants.UID, userUid);
        HomeCommunityRetailFragment fragment = new HomeCommunityRetailFragment();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void onDestroy() {

        super.onDestroy();
    }

    @Override
    public void onItemClick(int position) {

    }
}
